package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.GroupGoalAdapter;
import com.swz.dcrm.model.GroupGoal;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.ui.viewmodel.GroupViewModel;
import com.swz.dcrm.util.DateUtils;
import com.xh.baselibrary.callback.OnClickListener;

import java.util.Calendar;
import java.util.Date;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class TargetFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;

    @Inject
    GroupViewModel goalViewModel;


    private GroupGoalAdapter groupGoalAdapter;

    public static TargetFragment newInstance() {

        return new TargetFragment();
        // Required empty public constructor
    }
    @Override
    public void onLoadRetry() {
        Date date=DateUtils.dateParse(tvDate.getText().toString(),"yyyy年M月");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);


        goalViewModel.getSalesGroup(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1)
                .observe(this, response -> {
                    if(response.isSuccess()) {
                        groupGoalAdapter=new GroupGoalAdapter(getContext(),response.getData());
                        groupGoalAdapter.setOnClickListener(onClickListener);
                        rv.setAdapter(groupGoalAdapter);
                    }
                });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }
    @Override
    public int layoutId() {
        return R.layout.fragment_target;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.target_management_title));
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.setEnableRefresh(false);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        tvDate.setText(DateUtils.dateFormat(new Date(),"yyyy年M月"));
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }

    @OnClick({R.id.iv_pre,R.id.iv_next})
    public void change(View view){
        Date date=DateUtils.dateParse(tvDate.getText().toString(),"yyyy年M月");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        switch (view.getId()){
            case R.id.iv_pre:
                calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);

                break;
            case R.id.iv_next:
                calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);
        }
        tvDate.setText(DateUtils.dateFormat(calendar.getTime(),"yyyy年M月"));
        onLoadRetry();
    }


    OnClickListener<GroupGoal> onClickListener= group -> {
            EditStaffGoalFragmentArgs.Builder builder=new EditStaffGoalFragmentArgs.Builder();
            builder.setGroup(new Gson().toJson(group));
            go(null,R.id.action_targetFragment_to_editStaffGoalFragment,builder.build().toBundle());
    };
}
